package com.sq.universe.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.ResponseBody;


import java.io.IOException;
import java.nio.charset.Charset;

@Controller
@CrossOrigin
public class LoginController {

    @Value("classpath:templates/api/init.json")
    private Resource initJson;

    @Value("classpath:templates/api/clear.json")
    private Resource clearJson;

    @Value("classpath:templates/api/menus.json")
    private Resource menusJson;

    @Value("classpath:templates/api/table.json")
    private Resource tableJson;

    @Value("classpath:templates/api/tableSelect.json")
    private Resource tableSelectJson;

    @Value("classpath:templates/api/upload.json")
    private Resource uploadJson;

    @RequestMapping("api/upload")
    @ResponseBody
    public JSONObject upload(){
        String areaData = null;
        try {
            areaData = IOUtils.toString(uploadJson.getInputStream(), Charset.forName("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (JSONObject) JSON.parse(areaData);
    }

    @RequestMapping("api/tableSelect")
    @ResponseBody
    public JSONObject tableSelect(){
        String areaData = null;
        try {
            areaData = IOUtils.toString(tableSelectJson.getInputStream(), Charset.forName("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (JSONObject) JSON.parse(areaData);
    }

    @RequestMapping("api/table")
    @ResponseBody
    public JSONObject table(){
        String areaData = null;
        try {
            areaData = IOUtils.toString(tableJson.getInputStream(), Charset.forName("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (JSONObject) JSON.parse(areaData);
    }

    @RequestMapping("api/menus")
    @ResponseBody
    public JSONObject menus(){
        String areaData = null;
        try {
            areaData = IOUtils.toString(menusJson.getInputStream(), Charset.forName("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (JSONObject) JSON.parse(areaData);
    }

    @RequestMapping("api/init")
    @ResponseBody
    public JSONObject init(){
        String areaData = null;
        try {
            areaData = IOUtils.toString(initJson.getInputStream(), Charset.forName("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (JSONObject) JSON.parse(areaData);
    }

    @RequestMapping("api/clear")
    @ResponseBody
    public JSONObject clear(){
        String areaData = null;
        try {
            areaData = IOUtils.toString(clearJson.getInputStream(), Charset.forName("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (JSONObject) JSON.parse(areaData);
    }

    @RequestMapping("/login")
    public String login(){
        return "page/login-1";
    }

    @RequestMapping("/welcome")
    public String welcome(){
        return "page/welcome-1";
    }


    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/tableSelectPage")
    public String tableSelectPage(){
        return "page/table-select";
    }
}
